<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>Flutter架构 我的provider实施指南 | 早起的年轻人</title>
    <meta property="og:title" content="Flutter架构 我的provider实施指南 - 早起的年轻人">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-08-20T23:47:46&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-08-20T23:47:46&#43;08:00'>
        
    <meta name="Keywords" content="Flutter，golang,go语言,go语言笔记,飞雪无情,java,android,博客,项目管理,python,软件架构,公众号,小程序">
    <meta name="description" content="Flutter架构 我的provider实施指南">
        
    <meta name="author" content="luckly">
    <meta property="og:url" content="https://luckly.work/post/Flutter/flutter%E6%9E%B6%E6%9E%84-%E6%88%91%E7%9A%84provider%E5%AE%9E%E6%96%BD%E6%8C%87%E5%8D%97/">
    <link rel="shortcut icon" href='/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/css/normalize.css'>
    <link rel="stylesheet" href='/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/css/douban.css'>
    
        <link rel="stylesheet" href='/css/other.css'>
    
</head>

<link rel="stylesheet"
      href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://luckly.work/">
                        早起的年轻人
                    </a>
                
                <p class="description">专注于Flutter、Android、Java、Go语言(golang)、移动互联网、项目管理、软件架构</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://luckly.work/">首页</a>
                    
                    <a  href="https://luckly.work/categories/" title="分类">分类</a>
                    
                    <a  href="https://luckly.work/tags/" title="标签">标签</a>
                    
                    <a  href="https://luckly.work/archives/" title="归档">归档</a>
                    
                    <a  href="https://luckly.work/about/" title="关于我">关于我</a>
                    
                    <a  href="https://github.com/ITmxs/" title="github">github</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    <style type="text/css">
    .post-toc {
        position: fixed;
        width: 200px;
        margin-left: -210px;
        padding: 5px 10px;
        font-family: Athelas, STHeiti, Microsoft Yahei, serif;
        font-size: 12px;
        border: 1px solid rgba(0, 0, 0, .07);
        border-radius: 5px;
        background-color: rgba(255, 255, 255, 0.98);
        background-clip: padding-box;
        -webkit-box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        box-shadow: 1px 1px 2px rgba(0, 0, 0, .125);
        word-wrap: break-word;
        white-space: nowrap;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        z-index: 999;
        cursor: pointer;
        max-height: 70%;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .post-toc .post-toc-title {
        width: 100%;
        margin: 0 auto;
        font-size: 20px;
        font-weight: 400;
        text-transform: uppercase;
        text-align: center;
    }

    .post-toc .post-toc-content {
        font-size: 15px;
    }

    .post-toc .post-toc-content>nav>ul {
        margin: 10px 0;
    }

    .post-toc .post-toc-content ul {
        padding-left: 20px;
        list-style: square;
        margin: 0.5em;
        line-height: 1.8em;
    }

    .post-toc .post-toc-content ul ul {
        padding-left: 15px;
        display: none;
    }

    @media print,
    screen and (max-width:1057px) {
        .post-toc {
            display: none;
        }
    }
</style>
<div class="post-toc" style="position: absolute; top: 188px;">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
        <nav id="TableOfContents">
  <ul>
    <li><a href="#上下文">上下文</a></li>
    <li><a href="#一般概述">一般概述</a></li>
    <li><a href="#高级架构概述">高级架构概述</a></li>
    <li><a href="#代码设置">代码设置</a></li>
    <li><a href="#实施">实施</a>
      <ul>
        <li><a href="#导航">导航</a></li>
        <li><a href="#setstate-的共享-viewmodel-功能">setState 的共享 ViewModel 功能</a></li>
        <li><a href="#所有视图的共享设置">所有视图的共享设置</a></li>
      </ul>
    </li>
    <li><a href="#-架构和代码设置完成-">🎉 架构和代码设置完成 🎉</a>
      <ul>
        <li><a href="#使用具有自己模型的小部件">使用具有自己模型的小部件</a></li>
      </ul>
    </li>
  </ul>
</nav>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var postToc = $(".post-toc");
        if (postToc.length) {
            var leftPos = $("#main").offset().left;
            if(leftPos<220){
                postToc.css({"width":leftPos-10,"margin-left":(0-leftPos)})
            }

            var t = postToc.offset().top - 20,
                a = {
                    start: {
                        position: "absolute",
                        top: t
                    },
                    process: {
                        position: "fixed",
                        top: 20
                    },
                };
            $(window).scroll(function () {
                var e = $(window).scrollTop();
                e < t ? postToc.css(a.start) : postToc.css(a.process)
            })
        }
    })
</script>
    <article class="post">
        <header>
            <h1 class="post-title">Flutter架构 我的provider实施指南</h1>
        </header>
        <date class="post-meta meta-date">
            2021年8月20日
        </date>
        
        <div class="post-meta">
            <span>|</span>
            
            <span class="meta-category"><a href='/categories/Flutter'>Flutter</a></span>
            
        </div>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <blockquote>
<p>本文主要介绍Flutter Provider 架构实现。使用 Provider 实现生产就绪应用程序的明确指南和编码约定。</p>
</blockquote>
<h2 id="上下文">上下文</h2>
<p>围绕状态管理的 Flutter 架构一直是一个被丢弃的话题。使用各种技术构建了一些移动应用程序后，我发现无论您选择哪种架构或状态管理解决方案，某些原则都会并且应该存在。在本指南中，我将向您展示代码结构、命名约定、放置文件的位置、如何使用 Provider 包保持组织良好和易于维护。</p>
<h2 id="一般概述">一般概述</h2>
<p>Provider 基本上是 ScopedModel v2。我已经在 Redux、BLoC 和 ScopedModel 中实现了应用程序，我仍然认为 ScopedModel 是在 Flutter 中构建应用程序的最实用和最直接的方法。即使对于大型或大型应用程序(假设您遵循一些编码指南)。本指南的设置与我的<a href="https://www.filledstacks.com/post/flutter-architecture-scoped-model-implementation-guide">ScopedModel 指南</a>非常相似。让我们看看我们正在构建的应用程序，以便我们可以了解一些上下文。</p>
<p>
        <a data-fancybox="gallery" href="https://www.filledstacks.com/assets/static/010-all-screens.42db587.33b003e8aedccaf6c90f5b46dc5c0e52.jpg">
            <img class="mx-auto" alt="Flutter Provder 应用程序所有屏幕" src="https://www.filledstacks.com/assets/static/010-all-screens.42db587.33b003e8aedccaf6c90f5b46dc5c0e52.jpg" />
        </a>
    </p>
<p>该应用程序将具有三个屏幕。登录、主页和发布详细信息。我们将与<a href="https://jsonplaceholder.typicode.com/">JSONPlaceholder API</a>通信，使用输入的 ID 从登录中获取用户配置文件。获取并显示主页视图上的帖子，并显示帖子详细信息，并附加获取以显示评论。非常基本，但它涵盖了构建向您展示方式的架构所需的一切。</p>
<p><em>注意：我不会添加工具栏，所以 iOS 开发者可以在屏幕上从左向右滑动返回。或者，如果您愿意，只需添加一个 AppBar。将其添加到每个 Scaffold 中。</em></p>
<h2 id="高级架构概述">高级架构概述</h2>
<ul>
<li>每个视图都有自己的模型来扩展 ChangeNotifier。</li>
<li>只有当视图的状态发生变化时，才会调用视图的通知侦听器。</li>
<li>每个视图只有 2 个状态。空闲和忙碌。视图中包含的任何其他需要逻辑和状态/UI 更新的 UI 将具有与其关联的自己的模型。这样主视图只在主视图状态改变时绘制。</li>
<li>Providers 不会通过应用程序级别的全局提供程序传递，除非应用程序架构中的 1 个以上视图(用户信息)需要它。</li>
<li>提供者和服务将使用<a href="https://pub.dev/packages/get_it">get_it</a>注入。</li>
<li>模型只会从服务请求数据并从该数据减少状态。没有别的。</li>
<li>专用服务(只是普通对象，不要混淆初学者)将执行所有实际工作。Api 类将请求和序列化数据。模型只会调用函数来做到这一点。身份验证服务将使用 Api 获取用户详细信息并对其进行跟踪。该模型只是调用该函数并将值传递给它。</li>
</ul>
<p>这就是高水平。随着我们的进步，请记住这一点。</p>
<h2 id="代码设置">代码设置</h2>
<p>为了让教程只关注架构，我在 010 下的<a href="https://github.com/FilledStacks/flutter-tutorials">教程</a>仓库中设置了一个起始项目。如果你想跟我一起学习，请克隆这个仓库。我在这里添加的东西与架构无关。其中一些内容包括：</p>
<ul>
<li>使用 http 客户端从<a href="https://jsonplaceholder.typicode.com/">JSONPlaceholder</a>获取数据的 Api 服务</li>
<li>应用程序中所需的所有数据模型。评论，发布和用户。</li>
<li>两种颜色，BackgroundColor 和 CommentColor。</li>
<li>两种文字样式，headerStyle、subHeaderStyle</li>
<li>提供垂直间距的 Ui Helpers 类</li>
<li>带有空构建方法的 LoginView、HomeView 和 PostView。</li>
<li>用于呈现单个评论的小部件</li>
<li>用于呈现登录标题和文本字段的小部件</li>
<li>呈现单个帖子的小部件</li>
<li><a href="https://www.filledstacks.com/snippet/dependency-injection-in-flutter">像这样的</a>定位器文件设置</li>
</ul>
<p>让我们快速浏览一下结构。lib文件夹分为两个文件夹。核心和用户界面。Core 包含与逻辑关联的所有文件。ui 包含与 ui 关联的所有文件。Core分为三个文件夹。</p>
<ul>
<li>模型：包含所有普通数据模型</li>
<li>服务：包含将处理实际业务逻辑的专用文件</li>
<li>ViewModels：包含每个 Widget 视图的 Provider 模型</li>
</ul>
<p>UI 也分为三个文件夹。</p>
<ul>
<li>共享：包含在多个其他 UI 文件中使用的文件</li>
<li>视图：包含应用程序视图的文件</li>
<li>小部件：包含太大而无法保存在视图文件中的小部件文件。</li>
</ul>
<h2 id="实施">实施</h2>
<p>我们将逐个查看应用程序并添加我们需要的内容。让我们从登录屏幕开始。转到 core 下的 viewmodels 文件夹并添加一个名为 login_model.dart 的新文件。我们知道我们的视图只有 2 个状态，所以我们将在同一个文件中添加 ViewState 枚举。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/foundation.dart&#39;</span>;

<span style="color:#75715e">/// Represents the state of the view
</span><span style="color:#75715e"></span>enum ViewState { Idle, Busy }

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">LoginModel</span> <span style="color:#66d9ef">extends</span> ChangeNotifier {

}
</code></pre></div><p>现在把它连接起来。如前所述，我们不会在我们的应用程序开始时在全球范围内提供一堆模型和服务。相反，我们将使用 locator.dart 中的定位器设置注入它。使用定位器将 LoginModel 注册为惰性单例。阅读<a href="https://www.filledstacks.com/snippet/dependency-injection-in-flutter">本文</a>以快速了解不同类型的注册。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">void</span> setupLocator() {
  locator.registerLazySingleton(() <span style="color:#f92672">=&gt;</span> LoginModel());
}
</code></pre></div><p>转到 LoginView 并将此模型绑定到提供者/消费者。我们将脚手架包装在一个 Provider 中，我们将使 Provider 的孩子成为消费者，该消费者将 Scaffold 作为它的 builder 孩子返回。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider/provider.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider_architecutre/core/viewmodels/login_model.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider_architecutre/locator.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">LoginView</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> ChangeNotifierProvider<span style="color:#f92672">&lt;</span>LoginModel<span style="color:#f92672">&gt;</span>(
      builder: (context) <span style="color:#f92672">=&gt;</span> locator<span style="color:#f92672">&lt;</span>LoginModel<span style="color:#f92672">&gt;</span>(),
      child: Consumer<span style="color:#f92672">&lt;</span>LoginModel<span style="color:#f92672">&gt;</span>(
        builder: (context, model, child) <span style="color:#f92672">=&gt;</span> Scaffold(),
      ),
    );
  }
}
</code></pre></div><p>这里我们将 LoginModel 与 LoginView 相关联。让 UI 看起来像下面这样</p>
<p>
        <a data-fancybox="gallery" href="https://www.filledstacks.com/assets/static/010-login.df42093.b7943b278ad30d1e2648be0828920f4e.jpg">
            <img class="mx-auto" alt="Flutter Provider App 登录视图" src="https://www.filledstacks.com/assets/static/010-login.df42093.b7943b278ad30d1e2648be0828920f4e.jpg" />
        </a>
    </p>
<p>我们将 Scaffold 的背景颜色设置为 backgroundColor。脚手架的主体将是一个完全拉伸的 Colum，并将其子项对齐到中心。第一个子项是 LoginHeader，第二个子项是登录按钮。使您的构建方法如下所示并导入您需要的所有内容。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> ChangeNotifierProvider<span style="color:#f92672">&lt;</span>LoginModel<span style="color:#f92672">&gt;</span>(
      builder: (context) <span style="color:#f92672">=&gt;</span> locator<span style="color:#f92672">&lt;</span>LoginModel<span style="color:#f92672">&gt;</span>(),
      child: Consumer<span style="color:#f92672">&lt;</span>LoginModel<span style="color:#f92672">&gt;</span>(
        builder: (context, model, child) <span style="color:#f92672">=&gt;</span> Scaffold(
              backgroundColor: backgroundColor,
              body: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  LoginHeader(),
                  FlatButton(
                      color: Colors.white,
                      child: Text(
                        <span style="color:#e6db74">&#39;Login&#39;</span>,
                        style: TextStyle(color: Colors.black),
                      ),
                      onPressed: () <span style="color:#66d9ef">async</span> {})
                ],
              ),
            ),
      ),
    );
  }
</code></pre></div><p>我们需要 LoginView 来接收文本，为此我们需要一个 TextEdittingController，所以它需要是一个有状态的小部件。<strong>将视图转换为有状态小部件</strong>，创建控制器变量并将其传递到 LoginHeader。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">  <span style="color:#66d9ef">final</span> TextEditingController controller <span style="color:#f92672">=</span> TextEditingController();

  Widget build(BuildContext context) {
    ...
    LoginHeader(controller: controller),
    ...
  }
</code></pre></div><p>接下来，我们将把控制器中的值传递给名为 login 的模型上的函数。创建一个 Future，它在 LoginModel 上返回一个布尔值并传入一个字符串 userIdText。此函数将文本解析为 int 并将其传递给将发出 API 请求的身份验证服务。我们还想指出此时视图的状态。为此，我们将介绍 viewState 属性。每当我们更改此值时，我们都希望通知侦听器，以便重建视图。我们将创建一个 setState 函数来为我们处理这个问题。在通话开始时，我们希望将状态设置为忙碌，当我们完成后回到空闲状态。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">LoginModel</span> <span style="color:#66d9ef">extends</span> ChangeNotifier {
  <span style="color:#66d9ef">final</span> AuthenticationService _authenticationService <span style="color:#f92672">=</span> locator<span style="color:#f92672">&lt;</span>AuthenticationService<span style="color:#f92672">&gt;</span>();
  ViewState _state <span style="color:#f92672">=</span> ViewState.Idle;

  ViewState <span style="color:#66d9ef">get</span> state <span style="color:#f92672">=&gt;</span> _state;

  <span style="color:#66d9ef">void</span> setState(ViewState viewState) {
    _state <span style="color:#f92672">=</span> viewState;
    notifyListeners();
  }

   Future<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">bool</span><span style="color:#f92672">&gt;</span> login(<span style="color:#66d9ef">String</span> userIdText) <span style="color:#66d9ef">async</span> {
    setState(ViewState.Busy);

    <span style="color:#66d9ef">var</span> userId <span style="color:#f92672">=</span> <span style="color:#66d9ef">int</span>.tryParse(userIdText);
    <span style="color:#66d9ef">var</span> success <span style="color:#f92672">=</span>  <span style="color:#66d9ef">await</span> _authenticationService.login(userId);

    setState(ViewState.Idle);
    <span style="color:#66d9ef">return</span> success;
  }
}
</code></pre></div><p>AuthenticationService 将使用 Api 来获取用户配置文件。如果用户配置文件不为空，我们将返回 true 以表示成功。在 services 下创建 authentication_service.dart 文件并添加<code>Future&lt;bool&gt;</code>登录功能。此函数将使用 API 并请求用户配置文件。Api(以及所有其他服务和模型)将使用定位器注入。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;dart:async&#39;</span>;

<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider_architecutre/core/models/user.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider_architecutre/core/services/api.dart&#39;</span>;
<span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:provider_architecutre/locator.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">AuthenticationService</span> {
  <span style="color:#75715e">// Inject our Api
</span><span style="color:#75715e"></span>  Api _api <span style="color:#f92672">=</span> locator<span style="color:#f92672">&lt;</span>Api<span style="color:#f92672">&gt;</span>();

  Future<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">bool</span><span style="color:#f92672">&gt;</span> login(<span style="color:#66d9ef">int</span> userId) <span style="color:#66d9ef">async</span> {
    <span style="color:#75715e">// Get the user profile for id
</span><span style="color:#75715e"></span>    <span style="color:#66d9ef">var</span> fetcheduser <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> _api.getUserProfile(userId);

    <span style="color:#75715e">// Check if success
</span><span style="color:#75715e"></span>    <span style="color:#66d9ef">var</span> hasUser <span style="color:#f92672">=</span> fetcheduser <span style="color:#f92672">!=</span> <span style="color:#66d9ef">null</span>;

    <span style="color:#66d9ef">return</span> hasUser;
  }
}
</code></pre></div><p>使用定位器注册这两个服务。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">void</span> setupLocator() {
  locator.registerLazySingleton(() <span style="color:#f92672">=&gt;</span> AuthenticationService());
  locator.registerLazySingleton(() <span style="color:#f92672">=&gt;</span> Api());

  locator.registerLazySingleton(() <span style="color:#f92672">=&gt;</span> LoginModel());
}
</code></pre></div><p>现在，在按钮上 onPressed 的 LoginView 中，我们可以使用控制器中的文本调用登录函数。我们将等待 Future，如果成功，我们将导航到根视图。我们还想在视图繁忙时换出按钮。当视图繁忙时，我们将显示一个进度指示器。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">...

model.state <span style="color:#f92672">==</span> ViewState.Busy
  <span style="color:#f92672">?</span> CircularProgressIndicator()
  <span style="color:#f92672">:</span> FlatButton(
      color: Colors.white,
      child: Text(
        <span style="color:#e6db74">&#39;Login&#39;</span>,
        style: TextStyle(color: Colors.black),
      ),
      onPressed: () <span style="color:#66d9ef">async</span> {
        <span style="color:#66d9ef">var</span> loginSuccess <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> model.login(controller.text);
        <span style="color:#66d9ef">if</span> (loginSuccess) {
          <span style="color:#75715e">// Navigate to the home view
</span><span style="color:#75715e"></span>        }
      })
...
</code></pre></div><p>登录功能和业务逻辑完成。进入下一部分。</p>
<h3 id="导航">导航</h3>
<p>登录后，我们必须导航到主页视图。我最喜欢的新导航方式是使用<code>onGenerateRoute</code>MaterialApp 提供的功能。您必须为其提供一个函数，该函数将 RouteSettings 作为参数并返回一个<code>Route&lt;dynamic&gt;</code>. 为了保持整洁，我们将所有路由放在一个名为 &hellip;&hellip; router.dart 的单独文件中，位于 ui 文件夹下。创建路由器文件并放置一个带有我之前描述的签名的静态方法。此函数接收 RouteSettings，其中包含所请求的路线的名称。我们将打开该名称并为适当的视图返回 MaterialPageRoute。我们还将返回任何未定义路由的错误视图。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Router</span> {
  <span style="color:#66d9ef">static</span> Route<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">dynamic</span><span style="color:#f92672">&gt;</span> generateRoute(RouteSettings settings) {
    <span style="color:#66d9ef">switch</span> (settings.name) {
      <span style="color:#66d9ef">case</span> <span style="color:#e6db74">&#39;/&#39;</span><span style="color:#f92672">:</span>
        <span style="color:#66d9ef">return</span> MaterialPageRoute(builder: (_) <span style="color:#f92672">=&gt;</span> HomeView());
      <span style="color:#66d9ef">case</span> <span style="color:#e6db74">&#39;login&#39;</span><span style="color:#f92672">:</span>
        <span style="color:#66d9ef">return</span> MaterialPageRoute(builder: (_) <span style="color:#f92672">=&gt;</span> LoginView());
      <span style="color:#66d9ef">case</span> <span style="color:#e6db74">&#39;post&#39;</span><span style="color:#f92672">:</span>
        <span style="color:#66d9ef">return</span> MaterialPageRoute(builder: (_) <span style="color:#f92672">=&gt;</span> PostView());
      <span style="color:#66d9ef">default</span><span style="color:#f92672">:</span>
        <span style="color:#66d9ef">return</span> MaterialPageRoute(builder: (_) {
          <span style="color:#66d9ef">return</span> Scaffold(
            body: Center(
              child: Text(<span style="color:#e6db74">&#39;No route defined for </span><span style="color:#e6db74">${</span>settings.name<span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;</span>),
            ),
          );
        });
    }
  }
}
</code></pre></div><p>现在，在您的主文件中，您可以为该<code>onGenerateRoute</code>属性提供来自路由器的静态 generateRoute 函数。您可以删除 home 属性并将 initialRoute 设置为“登录”。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">MaterialApp(
    title: <span style="color:#e6db74">&#39;Flutter Demo&#39;</span>,
    theme: ThemeData(),
    initialRoute: <span style="color:#e6db74">&#39;login&#39;</span>,
    onGenerateRoute: Router.generateRoute,
  );
</code></pre></div><p>在登录按钮 onPressed 的 LoginView 中，我们现在可以通过推送命名路由导航到主视图。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">...

onPressed: () <span style="color:#66d9ef">async</span> {
    <span style="color:#66d9ef">var</span> loginSuccess <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> model.login(controller.text);
    <span style="color:#66d9ef">if</span> (loginSuccess) {
      Navigator.pushNamed(context, <span style="color:#e6db74">&#39;/&#39;</span>);
    }
  }

...
</code></pre></div><p>导航完成。视图模型绑定完成。依赖注入连接，代码结构布局。那么现在呢？好吧，我们必须重构代码以方便我们添加更多具有相同功能的视图，所以让我们这样做。</p>
<h3 id="setstate-的共享-viewmodel-功能">setState 的共享 ViewModel 功能</h3>
<p>我们所有的模型都将工作相同。我们有一个 state 属性，它告诉我们要在视图中显示什么 UI 布局，以及当它更新时，我们要调用 notifyListeners 以便将其移动到 BaseModel 中。在视图模型下创建一个名为 base_model.dart 的文件。将所有与状态相关的代码从登录模型移到那里。将 enum 定义移动到 viewstate.dart 下 core 中名为 enums 的文件中。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#75715e">// base_model.dart
</span><span style="color:#75715e"></span><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;package:flutter/material.dart&#39;</span>;

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">BaseModel</span> <span style="color:#66d9ef">extends</span> ChangeNotifier {
  ViewState _state <span style="color:#f92672">=</span> ViewState.Idle;

  ViewState <span style="color:#66d9ef">get</span> state <span style="color:#f92672">=&gt;</span> _state;

  <span style="color:#66d9ef">void</span> setState(ViewState viewState) {
    _state <span style="color:#f92672">=</span> viewState;
    notifyListeners();
  }
}
<span style="color:#75715e">// viewstate.dart
</span><span style="color:#75715e"></span>
<span style="color:#75715e">/// Represents the state of the view
</span><span style="color:#75715e"></span>enum ViewState { Idle, Busy }
</code></pre></div><p>将 LoginModel 更新为从 BaseModel 而不是 ChangeNotifier 扩展。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">LoginModel</span> <span style="color:#66d9ef">extends</span> BaseModel {
   ...
}
</code></pre></div><p>现在 LoginModel 更简洁一些，我们可以添加一些额外的功能。我们将处理一种错误情况。如果用户输入的值不是数字，我们将显示错误消息。我们将添加一个新的 String errorMessage 属性来存储我们的消息。</p>
<p>在 tryParse 代码下的登录函数中，我们将检查 userId 是否为空(不是数字)。如果是，我们会将消息和状态设置回空闲并返回 false。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">String</span> errorMessage;

...
<span style="color:#66d9ef">var</span> userId <span style="color:#f92672">=</span> <span style="color:#66d9ef">int</span>.tryParse(userIdText);

<span style="color:#66d9ef">if</span>(userId <span style="color:#f92672">==</span> <span style="color:#66d9ef">null</span>) {
  errorMessage <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;Value entered is not a number&#39;</span>;
  setState(ViewState.Idle);
  <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>;
}

...
</code></pre></div><p>在 Login 视图中，我们现在可以将 errorMessage 传递给 loginHeader 上的验证消息属性。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">...
LoginHeader(
    validationMessage: model.errorMessage,
    controller: _controller),
...
</code></pre></div><p>如果您现在输入一些文本并尝试登录，您将看到验证消息出现。</p>
<h3 id="所有视图的共享设置">所有视图的共享设置</h3>
<p>大多数视图都需要自己的模型，它们需要有一个根小部件 Provider 和一个采用构建方法的子 Consumer。我们将创建一个通用的 BaseView，它将为我们完成所有这些工作。在 ui 中的 views 文件夹下创建一个新的 base_view.dart 文件。将登录视图中的所有代码移到 Scaffold 之外，并将所有 LoginModel 类型替换为 T。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">BaseView</span><span style="color:#f92672">&lt;</span>T <span style="color:#66d9ef">extends</span> BaseModel<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#66d9ef">final</span> Widget Function(BuildContext context, T value, Widget child) builder;

  BaseView({<span style="color:#66d9ef">this</span>.builder});

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> ChangeNotifierProvider<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span>(
      builder: (context) <span style="color:#f92672">=&gt;</span> locator<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span>(),
      child: Consumer<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span>(builder: builder),
    );
  }
}
</code></pre></div><p>在 LoginView 中，用 BaseView 替换所有与 Provider 相关的代码，并将 LoginModel 作为类型传递。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> BaseView<span style="color:#f92672">&lt;</span>LoginModel<span style="color:#f92672">&gt;</span>(
        builder: (context, model, child) <span style="color:#f92672">=&gt;</span> Scaffold(
          ...
        ),
    );
  }
</code></pre></div><p>现在，任何需要模型的小部件都可以使用 BaseView 及其模型类型，并且它会在调用 setState 时对状态更新作出反应 🎉。这基本上是缺少一件的主要架构。我们希望架构设置能够在显示新视图时轻松支持调用函数，而无需将所有内容转换为有状态小部件。为了实现这一点，我们将使用 onInit 将我们的 BaseView 转换为有状态的小部件，将我们的模型传回我们可以执行的回调函数中使用。将 BaseView 转换为有状态小部件并将其传递<code>Function(T)</code>给我们，从而将模型返回给我们。我们将模型本地存储在 state 中，并在 initState 调用中检查是否有回调。如果我们这样做，我们将调用它并将我们的模型传递给它。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">BaseView</span><span style="color:#f92672">&lt;</span>T <span style="color:#66d9ef">extends</span> ChangeNotifier<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">extends</span> StatefulWidget {
  <span style="color:#66d9ef">final</span> Widget Function(BuildContext context, T value, Widget child) builder;
  <span style="color:#66d9ef">final</span> Function(T) onModelReady;

  BaseView({<span style="color:#960050;background-color:#1e0010">@</span>required <span style="color:#66d9ef">this</span>.builder, <span style="color:#66d9ef">this</span>.onModelReady});

  <span style="color:#960050;background-color:#1e0010">@</span>override
  _BaseViewState<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span> createState() <span style="color:#f92672">=&gt;</span> _BaseViewState<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span>();
}

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">_BaseViewState</span><span style="color:#f92672">&lt;</span>T <span style="color:#66d9ef">extends</span> ChangeNotifier<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">extends</span> State<span style="color:#f92672">&lt;</span>BaseView<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;&gt;</span> {
  T model <span style="color:#f92672">=</span> locator<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span>();

  <span style="color:#960050;background-color:#1e0010">@</span>override
  <span style="color:#66d9ef">void</span> initState() {
    <span style="color:#66d9ef">if</span> (widget.onModelReady <span style="color:#f92672">!=</span> <span style="color:#66d9ef">null</span>) {
      widget.onModelReady(model);
    }
    <span style="color:#66d9ef">super</span>.initState();
  }

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> ChangeNotifierProvider<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span>(
      builder: (context) <span style="color:#f92672">=&gt;</span> model,
      child: Consumer<span style="color:#f92672">&lt;</span>T<span style="color:#f92672">&gt;</span>(builder: widget.builder),
    );
  }
}
</code></pre></div><h2 id="-架构和代码设置完成-">🎉 架构和代码设置完成 🎉</h2>
<p>在这一点上，与架构有关的一切都已设置完毕。本教程的其余部分将向您展示如何使用此架构添加更多视图并扩展它。让我们构建 HomeView。</p>
<p>
        <a data-fancybox="gallery" href="https://www.filledstacks.com/assets/static/010-home.df42093.a684b3265175c998769e310496fcd544.jpg">
            <img class="mx-auto" alt="Flutter Provider App 主页视图" src="https://www.filledstacks.com/assets/static/010-home.df42093.a684b3265175c998769e310496fcd544.jpg" />
        </a>
    </p>
<p>让我们从<em>在 viewmodels 文件夹中</em>创建 home <em>model.dart 文件</em>开始*。****请记住****：_模型所做的只是使用服务来请求数据，然后使用该数据减少状态。*该模型会将 Api 注入其中并公开一个 List. 它还将有一个函数，用于获取帖子并将其设置为等于 posts 属性。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">HomeModel</span> <span style="color:#66d9ef">extends</span> BaseModel {
  Api _api <span style="color:#f92672">=</span> locator<span style="color:#f92672">&lt;</span>Api<span style="color:#f92672">&gt;</span>();

  List<span style="color:#f92672">&lt;</span>Post<span style="color:#f92672">&gt;</span> posts;

  Future getPosts(<span style="color:#66d9ef">int</span> userId) <span style="color:#66d9ef">async</span> {
    setState(ViewState.Busy);
    posts <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> _api.getPostsForUser(userId);
    setState(ViewState.Idle);
  }
}
</code></pre></div><p>然后使用定位器注册您的模型。我们将注册为工厂。这样做的作用是，每次您请求这种类型时，它都会创建一个新的请求。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">...
locator.registerFactory(() <span style="color:#f92672">=&gt;</span> HomeModel());
</code></pre></div><p>当主视图初始化时，我们想调用 getPosts 函数来为我们的用户获取帖子。为此，我们需要 userId，但目前没有。我们知道此视图和帖子详细信息视图中将需要用户信息以显示作者姓名。我们不想将 AuthenticationService 注入 home 模型，因为这在代码中没有意义。相反，我们将公开一个 User 类型的流控制器，我们将使用 StreamProvier 提供它。更新 AuthenticationService 并添加一个 StreamController 并且当用户不为空时将 fetchedUser 放到该控制器上。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">StreamController<span style="color:#f92672">&lt;</span>User<span style="color:#f92672">&gt;</span> userController <span style="color:#f92672">=</span> StreamController<span style="color:#f92672">&lt;</span>User<span style="color:#f92672">&gt;</span>();

Future<span style="color:#f92672">&lt;</span><span style="color:#66d9ef">bool</span><span style="color:#f92672">&gt;</span> login(<span style="color:#66d9ef">int</span> userId) <span style="color:#66d9ef">async</span> {
    <span style="color:#75715e">// Not real login, we&#39;ll just request the user profile
</span><span style="color:#75715e"></span>    <span style="color:#66d9ef">var</span> fetcheduser <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> _api.getUserProfile(userId);
    <span style="color:#66d9ef">var</span> hasUser <span style="color:#f92672">=</span> fetcheduser <span style="color:#f92672">!=</span> <span style="color:#66d9ef">null</span>;
    <span style="color:#66d9ef">if</span> (hasUser) {
      userController.add(fetcheduser);
    }

    <span style="color:#66d9ef">return</span> hasUser;
  }
</code></pre></div><p>现在在主文件中，我们将使用 StreamProvider 包装 MaterialApp，并从 AuthenticationService 传入我们的 streamController。我们将提供一个空用户作为初始数据。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyApp</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> StreamProvider<span style="color:#f92672">&lt;</span>User<span style="color:#f92672">&gt;</span>(
        initialData: User.initial(),
        builder: (context) <span style="color:#f92672">=&gt;</span> locator<span style="color:#f92672">&lt;</span>AuthenticationService<span style="color:#f92672">&gt;</span>().userController,
        child: MaterialApp(
          title: <span style="color:#e6db74">&#39;Flutter Demo&#39;</span>,
          theme: ThemeData(),
          initialRoute: <span style="color:#e6db74">&#39;login&#39;</span>,
          onGenerateRoute: Router.generateRoute,
        ));
  }
}
</code></pre></div><p>用户现在可以在应用程序的任何地方使用<code>Provider.of&lt;User&gt;(context)</code>. 在 Home 视图中，我们现在可以将根设置为 HomeModel 类型的 BaseView，并且 onModelReady 调用我们的 getPosts 函数。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">HomeView</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> BaseView<span style="color:#f92672">&lt;</span>HomeModel<span style="color:#f92672">&gt;</span>(
        onModelReady: (model) {
          model.getPosts(Provider.of<span style="color:#f92672">&lt;</span>User<span style="color:#f92672">&gt;</span>(context).id);
        },
        builder: (context, model, child) <span style="color:#f92672">=&gt;</span> Scaffold(
          backgroundColor: backgroundColor,
          body: Column()));
  }
}
</code></pre></div><p>对于主体，我们将根据状态值显示不同的 UI。忙碌时，我们将在屏幕中央显示一个 ProgressIndicator。当空闲时，我们将显示实际的 UI。它只是一个列，第一个子元素是 spaceContainer，第二个是标题，第三个也是标题，另一个是空格，然后是一个带有帖子列表的扩展小部件。</p>
<p>更新您的构建方法并添加如下所示的 listView 函数。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"> <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> BaseView<span style="color:#f92672">&lt;</span>HomeModel<span style="color:#f92672">&gt;</span>(
        onModelReady: (model) {
          model.getPosts(Provider.of<span style="color:#f92672">&lt;</span>User<span style="color:#f92672">&gt;</span>(context).id);
        },
        builder: (context, model, child) <span style="color:#f92672">=&gt;</span> Scaffold(
          backgroundColor: backgroundColor,
            body: model.state <span style="color:#f92672">==</span> ViewState.Idle
                <span style="color:#f92672">?</span> Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    UIHelper.verticalSpaceLarge(),
                    Padding(
                      padding: <span style="color:#66d9ef">const</span> EdgeInsets.only(left: <span style="color:#ae81ff">20.0</span>),
                      child: Text(<span style="color:#e6db74">&#39;Welcome </span><span style="color:#e6db74">${</span>Provider.of<span style="color:#f92672">&lt;</span>User<span style="color:#f92672">&gt;</span>(context).name<span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;</span>, style: headerStyle,),
                    ),
                    Padding(
                      padding: <span style="color:#66d9ef">const</span> EdgeInsets.only(left: <span style="color:#ae81ff">20.0</span>),
                      child: Text(<span style="color:#e6db74">&#39;Here are all your posts&#39;</span>, style: subHeaderStyle),
                    ),
                    UIHelper.verticalSpaceSmall(),
                    Expanded(child: getPostsUi(model.posts)),
                  ])
                <span style="color:#f92672">:</span> Center(child: CircularProgressIndicator())));
  }

  Widget getPostsUi(List<span style="color:#f92672">&lt;</span>Post<span style="color:#f92672">&gt;</span> posts) <span style="color:#f92672">=&gt;</span> ListView.builder(
      itemCount: posts.length,
      itemBuilder: (context, index) <span style="color:#f92672">=&gt;</span> PostListItem(
            post: posts[index],
            onTap: () {
              Navigator.pushNamed(context, <span style="color:#e6db74">&#39;post&#39;</span>, arguments: posts[index]);
            },
          ));
</code></pre></div><p>正如您在 getPostUi 函数中看到的那样。当 PostListItem 被点击时，我们导航到“post”，但我们向它传递了一些参数。参数是 post 模型。我们必须更新我们的路由器以查看参数并将其传递给 Post 视图。打开路由器并更新“post”案例以获取参数并转换为 Post。然后将其传递给 PostView。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">case</span> <span style="color:#e6db74">&#39;post&#39;</span><span style="color:#f92672">:</span>
  <span style="color:#66d9ef">var</span> post <span style="color:#f92672">=</span> settings.arguments <span style="color:#f92672">as</span> Post;
  <span style="color:#66d9ef">return</span> MaterialPageRoute(builder: (_) <span style="color:#f92672">=&gt;</span> PostView(post: post));
</code></pre></div><p>如果您现在运行代码，您将看到登录视图。输入 3 并点击登录。您应该看到忙碌指示器，然后导航到主页视图，在那里它也会显示忙碌，然后在获取帖子时显示 UI。</p>
<p>最后，我们要显示 Post 详细信息并获取一些其他评论。</p>
<h3 id="使用具有自己模型的小部件">使用具有自己模型的小部件</h3>
<p>评论将是一个独立的小部件，具有自己的模型和更新周期。这样，如果评论是实时的，使用类似 firebase 的东西，只有评论小部件会重建和重新绘制而不是整个屏幕。这实际上意味着 PostView 不需要模型，它没有逻辑。所以让我们创建评论模型，然后构建评论小部件，这样我们就可以完成帖子 ui。</p>
<p>在视图模型下创建一个名为 comments_model 的新文件。它与 HomeModel 几乎完全相同。使用 postId 获取评论，设置它，然后调用 setState。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CommentsModel</span> <span style="color:#66d9ef">extends</span> BaseModel {
  Api _api <span style="color:#f92672">=</span> locator<span style="color:#f92672">&lt;</span>Api<span style="color:#f92672">&gt;</span>();

  List<span style="color:#f92672">&lt;</span>Comment<span style="color:#f92672">&gt;</span> comments;

  Future fetchComments(<span style="color:#66d9ef">int</span> postId) <span style="color:#66d9ef">async</span> {
    setState(ViewState.Busy);
    comments <span style="color:#f92672">=</span> <span style="color:#66d9ef">await</span> _api.getCommentsForPost(postId);
    setState(ViewState.Idle);
  }
}
</code></pre></div><p>我们已经有了一个 Comments 文件，所以我们只需填写与我们设置的架构相关的部分。我们将使用类型为 CommentsModel 的 BaseView。onModelReady 我们将请求使用传入的 postId 获取评论。 UI 将与 HomeView 相同。忙碌时，循环指示器 else 展开的列表视图。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Comments</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#66d9ef">final</span> <span style="color:#66d9ef">int</span> postId;
  Comments(<span style="color:#66d9ef">this</span>.postId);

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> BaseView<span style="color:#f92672">&lt;</span>CommentsModel<span style="color:#f92672">&gt;</span>(
        onModelReady: (model) <span style="color:#f92672">=&gt;</span> model.fetchComments(postId),
        builder: (context, model, child) <span style="color:#f92672">=&gt;</span> model.state <span style="color:#f92672">==</span> ViewState.Busy
            <span style="color:#f92672">?</span> Center(child: CircularProgressIndicator())
            <span style="color:#f92672">:</span> Expanded(child: ListView(
                children: model.comments
                    .map((comment) <span style="color:#f92672">=&gt;</span> CommentItem(comment))
                    .toList(),
              )));
  }
}
</code></pre></div><p>在定位器中注册它。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart">locator.registerFactory(() <span style="color:#f92672">=&gt;</span> CommentsModel());
</code></pre></div><p>现在让我们构建 PostView ui。</p>
<p>
        <a data-fancybox="gallery" href="https://www.filledstacks.com/assets/static/010-post.df42093.6a3125dd7be5051f7ce361d63893316e.jpg">
            <img class="mx-auto" alt="Flutter 提供程序应用发布视图" src="https://www.filledstacks.com/assets/static/010-post.df42093.6a3125dd7be5051f7ce361d63893316e.jpg" />
        </a>
    </p>
<p>非常简单的用户界面。顶部有一些间距的标题，作者姓名，帖子正文，然后是我们的评论小部件。这里没什么好看的。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-dart" data-lang="dart"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">PostView</span> <span style="color:#66d9ef">extends</span> StatelessWidget {
  <span style="color:#66d9ef">final</span> Post post;
  PostView({<span style="color:#66d9ef">this</span>.post});

  <span style="color:#960050;background-color:#1e0010">@</span>override
  Widget build(BuildContext context) {
    <span style="color:#66d9ef">return</span> Scaffold(
      backgroundColor: backgroundColor,
      body: Padding(
        padding: <span style="color:#66d9ef">const</span> EdgeInsets.symmetric(horizontal: <span style="color:#ae81ff">20.0</span>),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <span style="color:#f92672">&lt;</span>Widget<span style="color:#f92672">&gt;</span>[
            UIHelper.verticalSpaceLarge(),
            Text(post.title, style: headerStyle),
            Text(
              <span style="color:#e6db74">&#39;by </span><span style="color:#e6db74">${</span>Provider.of<span style="color:#f92672">&lt;</span>User<span style="color:#f92672">&gt;</span>(context).name<span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;</span>,
              style: TextStyle(fontSize: <span style="color:#ae81ff">9.0</span>),
            ),
            UIHelper.verticalSpaceMedium(),
            Text(post.body),
            Comments(post.id)
          ],
        ),
      ),
    );
  }
}
</code></pre></div><p>就是这样。我们有一个完整的架构，绝对可以承受“大应用”的评论。我重新编写了我在 Xamarin 中编写的一个非常大的应用程序，这就是我正在使用的架构。它整洁且易于遵循。大多数模型的代码少于 20 行。服务也很少，因此很容易扩展。</p>
<p>我希望这有帮助。还有很多其他的东西要添加，但这是您想要开始的地方。更进一步，您可以决定要创建的新文件夹，在 viewname 文件夹下将小部件分组以将其保持在一起，将服务拆分为实用程序和服务等。一切由您决定，至少您现在有一个更好的起点。</p>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://luckly.work/">luckly</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://luckly.work/post/Flutter/flutter%E6%9E%B6%E6%9E%84-%E6%88%91%E7%9A%84provider%E5%AE%9E%E6%96%BD%E6%8C%87%E5%8D%97/">https://luckly.work/post/Flutter/flutter%E6%9E%B6%E6%9E%84-%E6%88%91%E7%9A%84provider%E5%AE%9E%E6%96%BD%E6%8C%87%E5%8D%97/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/post/Flutter/flutter%E9%9D%A2%E8%AF%95%E9%A2%981/">Flutter面试题1</a></li>
        
        <li><a href="/post/flutter_tips/Flutter%E5%B8%B8%E7%94%A8%E7%9A%84%E5%BF%AB%E6%8D%B7%E9%94%AE-AS/">《Flutter常用的快捷键 AS》</a></li>
        
        <li><a href="/post/flutter_tips/Flutter%E5%AF%B9list%E6%8E%92%E5%BA%8F/">《如何根据函数返回的值对dart中的List进行排序》</a></li>
        
        <li><a href="/post/flutter_widget/%E8%87%AA%E5%AE%9A%E4%B9%89widget/">自定义widget</a></li>
        
        <li><a href="/post/flutter_tips/Flutter%E4%BD%BF%E7%94%A8Spread%E5%87%8F%E5%B0%91%E4%BB%A3%E7%A0%81%E8%A1%8C/">Flutter使用Spread减少代码行</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/tags/Flutter'>Flutter</a></li>
                
            </ul>
            
        </div>
    </article>
    
    

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "https://github.com/ITmxs/repo"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2021 <a href="https://luckly.work/">早起的年轻人 By luckly</a>
        
        | <a rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">粤ICP备2021号-1</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://juejin.cn/user/3843548384077192" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">掘金</span></a>
        </div>
        <div class="github-badge">
            <a href="https://space.bilibili.com/480883651" target="_black"><span class="badge-subject">Design by</span><span class="badge-value bg-brightgreen">早起的年轻人</span></a>
        </div>
        <div class="github-badge">
            <a href="https://cloud.tencent.com/developer/user/6702670" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">云社区</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/js/douban.js'></script>
    <script src="/js/copy-to-clipboard.js"></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://luckly.work/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://luckly.work/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9/" title="Nginx文件内容">Nginx文件内容</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/nginx%E6%9E%81%E7%AE%80%E6%95%99%E7%A8%8B/" title="Nginx极简教程">Nginx极简教程</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/nginx/%E5%8D%81%E5%88%86%E9%92%9F%E5%85%A5%E9%97%A8nginx/" title="十分钟入门Nginx">十分钟入门Nginx</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/go/Goland%E8%BF%9C%E7%A8%8B%E5%BC%80%E5%8F%91%E9%85%8D%E7%BD%AE/" title="Goland远程开发配置">Goland远程开发配置</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E8%AF%BB%E4%B9%A6/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" title="域名解析">域名解析</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/git/%E6%8F%90%E4%BA%A4%E8%BF%87%E7%A8%8B%E7%9C%81%E7%95%A5%E6%9F%90%E4%BA%9B%E6%96%87%E4%BB%B6/" title="提交过程省略某些文件">提交过程省略某些文件</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_DropdownButton%E7%A4%BA%E4%BE%8B/" title="Flutter_DropdownButton示例">Flutter_DropdownButton示例</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter_ExpansionPanelList%E5%92%8CExpansionPanelList.radio%E7%A4%BA%E4%BE%8B/" title="Flutter_ExpansionPanelList和ExpansionPanelList">Flutter_ExpansionPanelList和ExpansionPanelList</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/flutter_tips/Flutter%E5%BE%AE%E4%BF%A1%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E8%B7%B3%E5%9B%9EApp%E6%8C%87%E5%AE%9A%E9%A1%B5%E9%9D%A2/" title="Flutter微信分享链接跳回App指定页面">Flutter微信分享链接跳回App指定页面</a>
    </li>
    
    <li>
        <a href="https://luckly.work/post/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F/%E5%A4%96%E5%8C%85%E5%8F%AF%E8%83%BD%E7%9A%84%E9%97%AE%E9%A2%98/" title="外包可能的问题">外包可能的问题</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/categories/'>分类</a></h3>
<ul class="widget-list">
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (326)</a></li>
    
    <li><a href="https://luckly.work/categories/IT/">IT (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Kotlin/">Kotlin (2)</a></li>
    
    <li><a href="https://luckly.work/categories/Mysql/">Mysql (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (1)</a></li>
    
    <li><a href="https://luckly.work/categories/Vue/">Vue (6)</a></li>
    
    <li><a href="https://luckly.work/categories/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/android/">android (6)</a></li>
    
    <li><a href="https://luckly.work/categories/dart/">dart (96)</a></li>
    
    <li><a href="https://luckly.work/categories/Flutter/">Flutter (28)</a></li>
    
    <li><a href="https://luckly.work/categories/gin/">gin (25)</a></li>
    
    <li><a href="https://luckly.work/categories/git/">git (4)</a></li>
    
    <li><a href="https://luckly.work/categories/Go/">Go (102)</a></li>
    
    <li><a href="https://luckly.work/categories/gorm/">gorm (4)</a></li>
    
    <li><a href="https://luckly.work/categories/grpc/">grpc (1)</a></li>
    
    <li><a href="https://luckly.work/categories/html/">html (3)</a></li>
    
    <li><a href="https://luckly.work/categories/ios/">ios (1)</a></li>
    
    <li><a href="https://luckly.work/categories/linux/">linux (1)</a></li>
    
    <li><a href="https://luckly.work/categories/nginx/">nginx (6)</a></li>
    
    <li><a href="https://luckly.work/categories/python/">python (35)</a></li>
    
    <li><a href="https://luckly.work/categories/read/">读书笔记 (6)</a></li>
    
    <li><a href="https://luckly.work/categories/redis/">redis (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E5%8D%95/">书单 (8)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%B9%A6%E8%AF%84/">书评 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师 (19)</a></li>
    
    <li><a href="https://luckly.work/categories/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师 (25)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E5%88%99/">原则 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%9F%BA%E7%A1%80/">基础 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E5%BF%83%E8%AF%AD/">心语 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%8F%92%E4%BB%B6/">插件 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E6%95%99%E5%AD%A6/">教学 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%B2%BE%E8%BF%9B/">精进 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E7%BC%96%E7%A8%8B/">编程 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%99%9A%E5%B9%BB/">虚幻 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路 (2)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%91%E6%AD%A5/">跑步 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%B7%AF%E7%BA%BF/">路线 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学 (3)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%A1%B9%E7%9B%AE/">项目 (1)</a></li>
    
    <li><a href="https://luckly.work/categories/%E9%AD%85%E5%8A%9B/">魅力 (1)</a></li>
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/IT/">IT</a>
    
    <a href="https://luckly.work/tags/Kotlin/">Kotlin</a>
    
    <a href="https://luckly.work/tags/Mysql/">Mysql</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/Vue/">Vue</a>
    
    <a href="https://luckly.work/tags/YouTube%E8%A7%86%E9%A2%91%E4%B8%8B%E8%BD%BD/">YouTube视频下载</a>
    
    <a href="https://luckly.work/tags/android/">android</a>
    
    <a href="https://luckly.work/tags/dart/">dart</a>
    
    <a href="https://luckly.work/tags/flutter/">flutter</a>
    
    <a href="https://luckly.work/tags/gin/">gin</a>
    
    <a href="https://luckly.work/tags/git/">git</a>
    
    <a href="https://luckly.work/tags/go/">go</a>
    
    <a href="https://luckly.work/tags/gorm/">gorm</a>
    
    <a href="https://luckly.work/tags/grpc/">grpc</a>
    
    <a href="https://luckly.work/tags/html/">html</a>
    
    <a href="https://luckly.work/tags/ios/">ios</a>
    
    <a href="https://luckly.work/tags/linux/">linux</a>
    
    <a href="https://luckly.work/tags/nginx/">nginx</a>
    
    <a href="https://luckly.work/tags/python/">python</a>
    
    <a href="https://luckly.work/tags/redis/">redis</a>
    
    <a href="https://luckly.work/tags/%E4%B8%AA%E4%BA%BA%E8%B5%84%E6%96%99/">个人资料</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E5%8D%95/">书单</a>
    
    <a href="https://luckly.work/tags/%E4%B9%A6%E8%AF%84/">书评</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统管理师</a>
    
    <a href="https://luckly.work/tags/%E4%BF%A1%E6%81%AF%E7%B3%BB%E7%BB%9F%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E5%B8%88/">信息系统项目管理师</a>
    
    <a href="https://luckly.work/tags/%E5%85%A5%E9%97%A8/">入门</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E5%88%99/">原则</a>
    
    <a href="https://luckly.work/tags/%E5%8E%9F%E7%94%9F%E9%80%9A%E8%AE%AF/">原生通讯</a>
    
    <a href="https://luckly.work/tags/%E5%9F%BA%E7%A1%80/">基础</a>
    
    <a href="https://luckly.work/tags/%E5%A4%8D%E5%88%A9%E6%95%88%E5%BA%94/">复利效应</a>
    
    <a href="https://luckly.work/tags/%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B/">安装教程</a>
    
    <a href="https://luckly.work/tags/%E5%B0%91%E6%9C%89%E4%BA%BA%E8%B5%B0%E7%9A%84%E8%B7%AF/">少有人走的路</a>
    
    <a href="https://luckly.work/tags/%E5%BF%83%E8%AF%AD/">心语</a>
    
    <a href="https://luckly.work/tags/%E6%8F%92%E4%BB%B6/">插件</a>
    
    <a href="https://luckly.work/tags/%E6%95%99%E5%AD%A6/">教学</a>
    
    <a href="https://luckly.work/tags/%E7%8E%8B%E9%98%B3%E6%98%8E/">王阳明</a>
    
    <a href="https://luckly.work/tags/%E7%94%B5%E5%AD%90%E4%B9%A6/">电子书</a>
    
    <a href="https://luckly.work/tags/%E7%99%BB%E9%99%86%E8%A1%A8%E5%8D%95/">登陆表单</a>
    
    <a href="https://luckly.work/tags/%E7%A8%BB%E7%9B%9B%E5%92%8C%E5%A4%AB/">稻盛和夫</a>
    
    <a href="https://luckly.work/tags/%E7%A9%B7%E7%88%B8%E7%88%B8%E5%AF%8C%E7%88%B8%E7%88%B8/">穷爸爸富爸爸</a>
    
    <a href="https://luckly.work/tags/%E7%B2%BE%E8%BF%9B/">精进</a>
    
    <a href="https://luckly.work/tags/%E7%BC%96%E7%A8%8B/">编程</a>
    
    <a href="https://luckly.work/tags/%E8%99%9A%E5%B9%BB/">虚幻</a>
    
    <a href="https://luckly.work/tags/%E8%AF%97/">诗</a>
    
    <a href="https://luckly.work/tags/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/">读书笔记</a>
    
    <a href="https://luckly.work/tags/%E8%B4%A2%E5%8A%A1%E8%87%AA%E7%94%B1%E4%B9%8B%E8%B7%AF/">财务自由之路</a>
    
    <a href="https://luckly.work/tags/%E8%B7%91%E6%AD%A5/">跑步</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%94%B1%E4%BC%A0%E5%8F%82/">路由传参</a>
    
    <a href="https://luckly.work/tags/%E8%B7%AF%E7%BA%BF/">路线</a>
    
    <a href="https://luckly.work/tags/%E8%BD%AF%E4%BB%B6%E5%AE%9E%E6%96%BD/">软件实施</a>
    
    <a href="https://luckly.work/tags/%E9%80%9A%E8%AE%AF%E5%BD%95/">通讯录</a>
    
    <a href="https://luckly.work/tags/%E9%98%B3%E6%98%8E%E5%BF%83%E5%AD%A6/">阳明心学</a>
    
    <a href="https://luckly.work/tags/%E9%A1%B9%E7%9B%AE/">项目</a>
    
    <a href="https://luckly.work/tags/%E9%AD%85%E5%8A%9B/">魅力</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="http://www.topgoer.com/" title="枯藤">枯藤</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gorm.cn/zh_CN/docs/index.html" title="gorm">gorm</a>
        </li>
        
        <li>
            <a target="_blank" href="https://docs.python.org/zh-cn/3/tutorial/index.html" title="python">python</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.liwenzhou.com/" title="李文周">李文周的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://www.xbzweb.com/" title="小包子的博客">小包子的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://www.flysnow.org/" title="飞雪无情的博客">飞雪无情的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://sliverhorn.com/" title="sliverhorn的博客">sliverhorn的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="http://yuedu.baidu.com/ebook/14a722970740be1e640e9a3e" title="Android Gradle权威指南">Android Gradle权威指南</a>
        </li>
        
        <li>
            <a target="_blank" href="https://gesdh.cn/" title="小格子">格子导航</a>
        </li>
        
        <li>
            <a target="_blank" href="https://itachi.xyz/" title="阿林">itachi&#39;s Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://darjun.github.io/" title="大俊">大俊Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="https://geektutu.com/post/quick-golang.html" title="极客兔兔">极客兔兔Blog</a>
        </li>
        
        <li>
            <a target="_blank" href="http://zxfcumtcs.github.io/" title="赵雪峰">雪峰Blog</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://luckly.work/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>